/*
 * @Descripttion: 
 * @version: 
 * @Author: guangyunl
 * @Date: 2024-01-19 22:32:40
 * @LastEditors: guangyunl
 * @LastEditTime: 2024-03-28 21:08:54
 */
/* indent size: 2 */

module.exports = app => {
    const DataTypes = app.Sequelize;
  
    const Orders_products = app.model.define('orders_products', {
      id: {
        type: DataTypes.INTEGER,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        comment: "订单-商品_中间表id"
      },
      pid: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '商品id',
      },
      pcount: {
        type: DataTypes.INTEGER,
        defaultValue: 1,
        comment: '商品数量'
      },
      sprice: {
        type: DataTypes.DECIMAL,
        comment: '商品活动价格'
      },
      oid: {
        type: DataTypes.INTEGER,
        allowNull: false,
        comment: '订单id',
        references: {
            model: 'orders',
            key: 'id'
          }
      },
      createdAt: {
        type: DataTypes.DATE,
        allowNull: true,
        comment: '创建时间'
      },
      updatedAt: { 
        type: DataTypes.DATE,
        allowNull: true,
        comment: '更新时间'
      }
    }, {
      tableName: 'orders_products'
    });
  
    Orders_products.associate = function() {
        // 产品属于订单
        app.model.Products.belongsToMany(app.model.Orders,
          {
            through: app.model.OrdersProducts,
            foreignKey: "pid",
            otherKey: "oid"
          })
        // 订单属于产品
        app.model.Orders.belongsToMany(app.model.Products,
          {
            through: app.model.OrdersProducts,
            foreignKey: "oid",
            otherKey: "pid"
          })
    }
  
    return Orders_products;
  };
  